<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of expense_obj
 *
 * @author Juraj
 */
class expense_obj {
    
    public function checkAdd_expense($check_data) {
        $result = array();
        foreach ($check_data as $key => $value) {
            switch ($key) {
                case 'user':
                    if (empty($value))
                        $result[] = 'user';
                    break;
                case 'fixed_expense':
                    $value = (double) $value;
                    if (empty($value))
                        $result[] = 'fixed_expense';
                    break;
                case 'from':
                    if (empty($value))
                        $result[] = 'from';
                    break;
            }
        }
        return $result;
    }
    
    public function addExpense($in_data) {
        if (empty($in_data)) {
            return new error_obj('empty $in_data', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        $result = $db_obj->insert($GLOBALS['tb_fixed_expense'], $in_data);
        if (error_obj::isError($result))
            return $result;
        else
            return $db_obj->lastInsertedId();
    }
    
    public function getExpensesGroupByUser() {
        $db_obj = new db_obj();
        
        $sql = "SELECT a.userId FROM " . $GLOBALS['tb_fixed_expense'] . " as a, " . $GLOBALS['tb_users'] . " as b WHERE b.users_id=a.userId GROUP BY a.userId ORDER BY b.surname";
        
        $users = $db_obj->getCol($sql);
        
        if (error_obj::isError($users)){
            return $users;
        } elseif (empty($users)) {
            return array();
        } else {
            $result = array();
            foreach ($users as $userId){
                $expenses = $this->getExpensesByUser($userId, '`from` DESC');
                if (error_obj::isError($expenses)){
                    continue;
                } else 
                    $result[$userId] = $expenses;
            }
            return $result;
        }
    }
    
    public function getExpensesByUser($in_userId, $order = '') {
        $userId = (int) $in_userId;
        if (empty($userId))
            return new error_obj ("empty in_userId", __CLASS__, __FUNCTION__);
        
        if (!empty ($order)){
            $order = "ORDER BY " . $order;
        }
        $db_obj = new db_obj();
        
        $sql = "SELECT * FROM " . $GLOBALS['tb_fixed_expense'] . " WHERE userId='".$userId."' $order";
        
        return $db_obj->getAll($sql);
    }
    
    public function deleteExpenseById($id) {
        $id = (int) $id;
        if (empty($id)){
            return new error_obj('empty $id', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        
        $sql = "DELETE FROM " . $GLOBALS['tb_fixed_expense'] . " WHERE id='".$id."'";
        
        return $db_obj->query($sql);
    }
    
    public function getExpenseById($id, $cols = array()) {
        $id = (int) $id;
        if (empty($id))
            return new error_obj('empty id', get_class(), __FUNCTION__);

        $what = '';
        if (!empty($cols)) {
            foreach ($cols as $value) {
                $what .= $value . ',';
            }
            $what = substr($what, 0, strlen($what) - 1);
        } else {
            $what .= '*';
        }

        $db_obj = new db_obj();
        return $db_obj->getRow("SELECT $what FROM " . $GLOBALS['tb_fixed_expense'] . " WHERE id='$id'");
    }
    
    public function editExpense($in_data, $id) {
        $id = (int) $id;
        if (empty($id)) {
            return new error_obj('empty $id', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        $keys = array('id' => $id);
        return $db_obj->update($GLOBALS['tb_fixed_expense'], $in_data, $keys);
    }
    
    public function getExpenseInPeriodByUser($userId, $period) {
        $userId = (int) $userId;
        if (empty($userId)) {
            return new error_obj('empty $userId', get_class(), __FUNCTION__);
        }
        
        if (empty($period)) {
            return new error_obj('empty period', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        $sql = "SELECT expense FROM " . $GLOBALS['tb_fixed_expense'] . " WHERE userId='$userId' AND `from`<='$period' ORDER BY `from` DESC LIMIT 1";
        
        return $db_obj->getOne($sql);
    }
    
    public function get_hours_per_servisman_on_task($userId, $month, $year, $taskId = 0) {
      $db_obj = new db_obj();  
      $sql = "SELECT SUM(amount) FROM " . $GLOBALS['tb_records'] . " WHERE type!=8 AND tariff!=6 AND user_id='" . $userId . "' AND DATE_FORMAT(date,'%m') = '" . $month . "' AND DATE_FORMAT(date,'%Y')='" . $year . "'";
      if ($taskId > 0)
          $sql .= " AND taskId='" . $taskId . "'";
      return $db_obj->getOne($sql);
    }
    
    public function get_hours_per_servisman($userId, $month, $year) {
      return $this->get_hours_per_servisman_on_task($userId, $month, $year);
    }
    
    public function get_hours_per_responsible_on_task($taskId, $clientId, $userId, $month, $year) {
      $db_obj = new db_obj(); 
      $sql = "SELECT SUM(amount) FROM " . $GLOBALS['tb_records'] . " WHERE type!=8 AND tariff!=6 AND taskId='" . $taskId . "' AND client='" . $clientId . "' AND user_id='" . $userId . "' AND DATE_FORMAT(date,'%m') = '" . $month . "' AND DATE_FORMAT(date,'%Y')='" . $year . "'";
      return $db_obj->getOne($sql);
    }
    
    public function get_hours_per_responsible($clientId, $userId, $month, $year) {
      $db_obj = new db_obj(); 
      $sql = "SELECT SUM(amount) FROM " . $GLOBALS['tb_records'] . " WHERE type!=8 AND client='" . $clientId . "' AND user_id='" . $userId . "' AND DATE_FORMAT(date,'%m') = '" . $month . "' AND DATE_FORMAT(date,'%Y')='" . $year . "'";
      return $db_obj->getOne($sql);
    }
    
    public function get_records_per_servisman_on_task($taskId, $month, $year, $userId = -1, $notEmptyTariff = false){
      $record_obj = new record_obj();
      if ($notEmptyTariff){
        $f = array(
          'taskId' => $taskId, // dana uloha 
          'nottariff' => 6, // nie pravidelny servis
          'nottariff' => -1, // nie zaznam bez tariffy
          'nottype' => 8, // nie dodany tovar
          'monthm' => $month, // dany mesiac
          'year' => $year // dany rok
        );
      } else {
        $f = array(
          'taskId' => $taskId, // dana uloha 
          'nottariff' => 6, // nie pravidelny servis
          'nottype' => 8, // nie dodany tovar
          'monthm' => $month, // dany mesiac
          'year' => $year // dany rok
        );
      }
      
      if ($userId != -1)
        $f['user_id'] = $userId;
      
      return $record_obj->getListOfRecords($f);
    }
}

?>
